package com.shop.generator;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import com.shop.core.BaseEntity;

import java.util.HashMap;
import java.util.Map;

public class Coding {
    public static void main(String[] args) {
        //数据库表名
        String table = "pms_product";
        //项目名
        String project = "product";

        // 获取父项目路径
        String parent_path = System.getProperty("user.dir");
        // 配置entity路径
        String entity_path = parent_path + "/entity/src/main/java/com/shop/entity";
        // 配置 mapper路径
        String mapper_path = parent_path + "/" + project + "/src/main/java/com/shop/mapper";
        // 配置 xml路径
        String xml_path = parent_path + "/" + project + "/src/main/resources/com/shop/mapper";
        // 配置 service路径
        String service_path = parent_path + "/" + project + "/src/main/java/com/shop/service";
        // 配置 service_impl路径
        String service_impl_path = parent_path + "/" + project + "/src/main/java/com/shop/service/impl";
        // 配置 controller 路径
        String controller_path = parent_path + "/" + project + "/src/main/java/com/shop/controller";
        // 创建生成器对象
        AutoGenerator mpg = new AutoGenerator();
        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        // 配置作者
        gc.setAuthor("lpx610");
        // 生成代码后不用打开目录
        gc.setOpen(false);
        // 去掉service接口的I
        gc.setServiceName("%sService");
        mpg.setGlobalConfig(gc);
        // 配置数据源
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setDbType(DbType.MYSQL);
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUrl("jdbc:mysql://39.106.73.160:3306/shop?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai");
        dsc.setUsername("root");
        dsc.setPassword("123456");
        mpg.setDataSource(dsc);
        // 包配置
        PackageConfig pc = new PackageConfig();
        // 配置父包
        pc.setParent("com.shop");
        // 自定义路径
        Map<String, String> pathInfo = new HashMap<>();
        pathInfo.put("entity_path", entity_path);
        pathInfo.put("mapper_path", mapper_path);
        pathInfo.put("xml_path", xml_path);
        pathInfo.put("service_path", service_path);
        pathInfo.put("service_impl_path", service_impl_path);
        pathInfo.put("controller_path", controller_path);
        pc.setPathInfo(pathInfo);

        mpg.setPackageInfo(pc);
        // 策略配置
        StrategyConfig sc = new StrategyConfig();
        // 表名生成entity类名的时候 下划线转驼峰
        sc.setNaming(NamingStrategy.underline_to_camel);
        // 类名 转 属性名的时候 下划线转驼峰
        sc.setColumnNaming(NamingStrategy.underline_to_camel);
        // 所有的entity 都加lombok注解
        sc.setEntityLombokModel(true);
        // 所有controller都加RestController注解
        sc.setRestControllerStyle(true);
        // Controller类上都加requestMapping 地址 也是驼峰
        sc.setControllerMappingHyphenStyle(false);
        // 让每个entity 都继承BaseEntity
        sc.setSuperEntityClass(BaseEntity.class);
        // 每个entity 都不再生成id
        sc.setSuperEntityColumns("id");
        // 要生成的表名
        sc.setInclude(table);
        mpg.setStrategy(sc);
        // 模板引擎配置
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        // 生成代码
        mpg.execute();
    }
}
